package com.guigu.ssyx.search.service.impl;

import com.atguigu.ssyx.enums.SkuType;
import com.atguigu.ssyx.model.product.Category;
import com.atguigu.ssyx.model.product.SkuInfo;
import com.atguigu.ssyx.model.search.SkuEs;
import com.guigu.ssyx.client.product.ProductFeignClient;
import com.guigu.ssyx.search.repository.SkuRepository;
import com.guigu.ssyx.search.service.SkuService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SkuServiceImpl  implements SkuService {

    private static final Logger log = LoggerFactory.getLogger(SkuServiceImpl.class);
    @Autowired
    private SkuRepository skuRepository;

    @Autowired
    private ProductFeignClient productFeignClient;


    /**
     * 上架商品
     * */
    @Override
    public void upperSku(Long skuId) {
        log.info("上架商品id：{}", skuId);

        SkuEs skuEs = new SkuEs();


        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
        if(skuInfo == null) return;

        //查询分类
        Category category = productFeignClient.getCategory(skuInfo.getCategoryId());

        if(category != null){
            skuEs.setCategoryId(category.getId());
            skuEs.setCategoryName(category.getName());
        }
        
        //封装数据
        skuEs.setId(skuInfo.getId());
        skuEs.setKeyword((skuInfo.getSkuName() + "," + skuEs.getCategoryName()));
        skuEs.setWareId(skuInfo.getWareId());
        skuEs.setIsNewPerson(skuInfo.getIsNewPerson());
        skuEs.setImgUrl(skuInfo.getImgUrl());
        skuEs.setTitle(skuInfo.getSkuName());

        if(skuInfo.getSkuType() == SkuType.COMMON.getCode()) {
            skuEs.setSkuType(0);
            skuEs.setPrice(skuInfo.getPrice().doubleValue());
            skuEs.setStock(skuInfo.getStock());
            skuEs.setSale(skuInfo.getSale());
            skuEs.setPerLimit(skuInfo.getPerLimit());
        } else {
            //TODO 待完善-秒杀商品

        }


        SkuEs save = skuRepository.save(skuEs);


    }

    /**
     * 下架商品
     * */
    @Override
    public void lowerSku(Long skuId) {
        skuRepository.deleteById(skuId);
    }
}
